import Vuex from 'vuex'
import request from '~/assets/request'

const state = {
  payload: {
    page: 1,
    tag: ''
  },
  article: [],
  total: 0,
  detail: {},
  tags: []
}

const getters = {}

const mutations = {
  // 通用store管理
  SET_STATE(state, payload) {
    Object.entries(payload).forEach(([key, value]) => {
      state[key] = value
    })
  }
}

const actions = {
  setState({
    commit
  }, data) {
    commit('SET_STATE', data)
  },
  async fetchTags({
    commit
  }) {
    const {
      data
    } = await request({
      url: '/api/tag'
    })
    commit('SET_STATE', {
      tags: data
    })
    return data
  }
}

const createStore = () => {
  return new Vuex.Store({
    state,
    getters,
    mutations,
    actions
  })
}

export default createStore
